/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucm.model;

import cp3120_p.DAO;
import javax.swing.JOptionPane;
import java.sql.*;

public class UCLogin_Model {

    private DAO DbConn;
    private String name;
    private String Password;

    public UCLogin_Model(String name, String Password) {
        this.name = name;
        this.Password = Password;
        DbConn = new DAO();
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean runLogin() {
        Connection con = DbConn.getConnection();
        if (con == null) {
            return false;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from userinfo where username = " + "'" + name + "'");
            res.first();
            if (Password.equals(res.getString("password"))) {
                return true;
            }
            JOptionPane.showMessageDialog(null, "PassWord is not cruuect");
            return false;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "no this user");
            return false;
        }
    }

    public boolean runLoginAdmin() {
        Connection con = DbConn.getConnection();
        if (con == null) {
            return false;
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            ResultSet res;
            res = stmt.executeQuery("select * from userinfo where username = " + "'" + name + "'");
            res.first();
            if (Password.equals(res.getString("password"))) {
                if (1 == res.getInt("ISADMIN")) {
                    return true;
                }
                 JOptionPane.showMessageDialog(null, "not admin");
                return false;
            }
            JOptionPane.showMessageDialog(null, "PassWord is not cruuect");
            return false;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "no this user");
            return false;
        }
    }
}
